#!/bin/sh
#
# $FreeBSD$
#

# REQUIRE: ix-warden jail
# PROVIDE: ix-plugins

. /etc/rc.freenas


plugins_start()
{
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT

	local args="$*"

	local sql="
	SELECT
		plugin_name,
		plugin_jail,
		plugin_path,
		plugin_enabled,
		plugin_port
	FROM
		plugins_plugins
	"

	if [ -n "${args}" ]
	then
		local where_args=""

		for p in ${args}
		do
			local jail="$(echo ${p}|cut -f1 -d':' -s)"
			local plugin="$(echo ${p}|cut -f2 -d':' -s)"
			if [ -z "${jail}" -o -z "${plugin}" ]
			then 
				continue
			fi

			if [ -z "${where_args}" ]
			then
				where_args="plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			else
				where_args="${where_args} OR
					plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			fi	
		done

		if [ -n "${where_args}" ]
		then
			sql="${sql} WHERE ( ${where_args} )"
		fi
	fi

	local _ifs="${IFS}"
	IFS="|"

	local tmpfile="/var/tmp/...borked"
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "${sql}" | \
	while read name jail path enabled port
	do
		if [ "${enabled}" != "1" ]
		then
			continue
		fi

		local jid=$(jail_get_jid "${jail}")
		if [ -z "${jid}" ]
		then
			continue
		fi

		local jail_ipv4
		local jail_ipv4_netmask
		local jail_ipv6
		local jail_ipv6_prefix

		local jail_path="$(jail_get_path "${jail}")"
		local jail_pbi_path=""${jail_path}/${path}/control.conf""

		: > "${jail_pbi_path}"

		local ipv4=$(jail_get_ipv4 "${jail}")
		if [ -n "${ipv4}" ]
		then
			jail_get_ip_and_netmask "${ipv4}"
			jail_ipv4="${JIP}"
		fi
		
		local ipv6=$(jail_get_ipv6 "${jail}")
		if [ -n "${ipv6}" ]
		then
			jail_get_ip_and_netmask "${ipv6}"
			jail_ipv6="${JIP}"
		fi

		cat <<-__EOF__>> "${jail_pbi_path}"
			ipv4: ${jail_ipv4}
			ipv6: ${jail_ipv6}
			port: ${port}
__EOF__

		echo jexec ${jid} "${path}/control" start "${jail_ipv4}" "${port}"

		jexec ${jid} "${path}/control" start "${jail_ipv4}" "${port}" 2>/dev/null
		if [ "$?" != "0" ]
		then
			touch "${tmpfile}"
		fi
	done
	IFS="${_ifs}"

	local res=0
	if [ -f "${tmpfile}" ]
	then
		rm -f "${tmpfile}"
		res=1
	fi

	return ${res}
}

plugins_stop()
{
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT

	local args="$*"

	sql="
	SELECT
		plugin_name,
		plugin_jail,
		plugin_path,
		plugin_enabled
	FROM
		plugins_plugins
	"

	if [ -n "${args}" ]
	then
		local where_args=""

		for p in ${args}
		do
			local jail="$(echo ${p}|cut -f1 -d':' -s)"
			local plugin="$(echo ${p}|cut -f2 -d':' -s)"
			if [ -z "${jail}" -o -z "${plugin}" ]
			then 
				continue
			fi

			if [ -z "${where_args}" ]
			then
				where_args="plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			else
				where_args="${where_args} OR
					plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			fi	
		done

		if [ -n "${where_args}" ]
		then
			sql="${sql} WHERE ( ${where_args} )"
		fi
	fi

	local _ifs="${IFS}"
	IFS="|"
	
	local tmpfile="/var/tmp/...borked"
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "${sql}" | \
	while read name jail path enabled 
	do
		if [ "${enabled}" != "1" ]
		then
			continue
		fi

		local jid=$(jail_get_jid "${jail}")
		if [ -z "${jid}" ]
		then
			continue
		fi

		echo jexec ${jid} "${path}/control" stop
		jexec ${jid} "${path}/control" stop 2>/dev/null
		if [ "$?" != "0" ]
		then
			touch "${tmpfile}"
		fi

		if [ "${rc_force}" = "yes" ]
		then
			jexec ${jid} ps -axw | \
				grep "${path}/control.py" | \
				grep -v grep | \
				awk '{ print $1 }' | \
				xargs kill -9 >/dev/null 2>&1
		fi
	done
	IFS="${_ifs}"

	local res=0
	if [ -f "${tmpfile}" ]
	then
		rm -f "${tmpfile}"
		res=1
	fi

	return ${res}
}

plugins_status()
{
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT

	local args="$*"

	sql="
	SELECT
		plugin_name,
		plugin_jail,
		plugin_path,
		plugin_enabled
	FROM
		plugins_plugins
	"

	if [ -n "${args}" ]
	then
		local where_args=""

		for p in ${args}
		do
			local jail="$(echo ${p}|cut -f1 -d':' -s)"
			local plugin="$(echo ${p}|cut -f2 -d':' -s)"
			if [ -z "${jail}" -o -z "${plugin}" ]
			then 
				continue
			fi

			if [ -z "${where_args}" ]
			then
				where_args="plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			else
				where_args="${where_args} OR
					plugin_jail = '${jail}' AND plugin_name = '${plugin}'"
			fi	
		done

		if [ -n "${where_args}" ]
		then
			sql="${sql} WHERE ( ${where_args} )"
		fi
	fi

	local _ifs="${IFS}"
	IFS="|"
	
	local tmpfile="/var/tmp/...sporked"
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "${sql}" | \
	while read name jail path enabled 
	do
		local jid=$(jail_get_jid "${jail}")
		if [ -z "${jid}" ]
		then
			continue
		fi

		jexec ${jid} env LD_LIBRARY_PATH=/usr/local/lib/compat "${path}/control" status 2>/dev/null
		if [ "$?" != "0" ]
		then
			touch "${tmpfile}"
		fi
	done
	IFS="${_ifs}"

	local res=0
	if [ -f "${tmpfile}" ]
	then
		rm -f "${tmpfile}"
		res=1
	fi

	return ${res}
}

name="ix-plugins"
start_cmd='plugins_start'
stop_cmd='plugins_stop'
status_cmd='plugins_status'
            
load_rc_config $name
run_rc_command $*
